package edu.ccut.saturn.studio.naviagtor;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.PerformanceStats;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.actions.ActionFactory;
import org.eclipse.ui.actions.NewProjectAction;
import org.eclipse.ui.navigator.CommonActionProvider;
import org.eclipse.ui.navigator.ICommonActionExtensionSite;
import org.eclipse.ui.navigator.ICommonMenuConstants;
import org.eclipse.ui.navigator.ICommonViewerWorkbenchSite;
import org.eclipse.ui.navigator.WizardActionGroup;

import edu.ccut.saturn.studio.core.util.action.SaturnAction;
import edu.ccut.saturn.studio.naviagtor.actions.NewSaturnActionAction;
import edu.ccut.saturn.studio.naviagtor.actions.NewSaturnComponentAction;
import edu.ccut.saturn.studio.naviagtor.actions.NewSaturnDataItemAction;
import edu.ccut.saturn.studio.naviagtor.actions.NewSaturnLogicAction;
import edu.ccut.saturn.studio.naviagtor.actions.NewSaturnPageAction;
import edu.ccut.saturn.studio.naviagtor.actions.NewSaturnProjectAction;
import edu.ccut.saturn.studio.naviagtor.actions.NewSaturnValidateAction;
import edu.ccut.saturn.studio.naviagtor.actions.NewSaturnWorkFlowAction;
import edu.ccut.saturn.studio.naviagtor.actions.model.CreateActionModelAction;
import edu.ccut.saturn.studio.naviagtor.actions.model.CreateComponentModelAction;
import edu.ccut.saturn.studio.naviagtor.actions.model.CreateDataModelAction;
import edu.ccut.saturn.studio.naviagtor.actions.model.CreateLogicModelAction;
import edu.ccut.saturn.studio.naviagtor.actions.model.CreatePageModelAction;
import edu.ccut.saturn.studio.naviagtor.actions.model.CreateSaturnSample;
import edu.ccut.saturn.studio.naviagtor.actions.model.CreateSaturnSampleAction1;
import edu.ccut.saturn.studio.naviagtor.actions.model.CreateValidateModelAction;
import edu.ccut.saturn.studio.naviagtor.actions.model.CreateWorkflowModelAction;

/**
 * 用于定义右键菜单内容
 * 
 * @author alf 2008-6-23
 */
public class NewActionProvider extends CommonActionProvider {

	private static final String NEW_MENU_NAME = "common.new.menu";

	private IAction showDlgAction;
	private IAction newProjectAction;
	private IAction newSaturnProjectAction;

	private WizardActionGroup newWizardActionGroup;

	private boolean isInital = false;

	private SaturnAction newSaturnActionAction;
	private SaturnAction newSaturnComponentAction;
	private SaturnAction newSaturnDataAction;
	private SaturnAction newSaturnLogicAction;
	private SaturnAction newSaturnPageAction;
	private SaturnAction newSaturnWorkFlowAction;
	private SaturnAction newSaturnValidateAction;

	private SaturnAction createActionModelAction;
	private SaturnAction createLogicModelAction;
	private SaturnAction createComponentModelAction;
	private SaturnAction createDataModelAction;
	private SaturnAction createValidateModelAction;
	private SaturnAction createWorkflowModelAction;
	private SaturnAction createPageModelAction;
	
	private SaturnAction CreateSaturnSample;

	public void init(ICommonActionExtensionSite anExtensionSite) {
		if (anExtensionSite.getViewSite() instanceof ICommonViewerWorkbenchSite) {
			IWorkbenchWindow window = ((ICommonViewerWorkbenchSite) anExtensionSite
					.getViewSite()).getWorkbenchWindow();

			showDlgAction = ActionFactory.NEW.create(window);

			newProjectAction = new NewProjectAction(window);
			newSaturnProjectAction = new NewSaturnProjectAction(window);

			newWizardActionGroup = new WizardActionGroup(window, PlatformUI
					.getWorkbench().getNewWizardRegistry(),
					WizardActionGroup.TYPE_NEW, anExtensionSite
							.getContentService());

			isInital = true;
			makeActions();
		}
	}

	@SuppressWarnings("restriction")
	public void fillContextMenu(IMenuManager menu) {
		IStructuredSelection selection = (IStructuredSelection) getContext()
				.getSelection();

		IMenuManager submenu = new MenuManager(
				org.eclipse.ui.internal.navigator.resources.plugin.WorkbenchNavigatorMessages.NewActionProvider_NewMenu_label,
				NEW_MENU_NAME);

		if (!isInital) {
			return;
		}

		submenu.add(newProjectAction);
		submenu.add(newSaturnProjectAction);
		submenu.add(new Separator());

		if (selection == null || selection.size() != 1) {
			submenu.add(new Separator());
			submenu.add(showDlgAction);
			menu.insertAfter(ICommonMenuConstants.GROUP_NEW, submenu);
			return;
		}

		Object object = ((IStructuredSelection) selection).getFirstElement();
		if (object instanceof IJavaElement) {
			IJavaElement javaElement = (IJavaElement) object;
			IResource iResource = javaElement.getResource();
			if (iResource instanceof IFolder) {
				IFolder iFolder = (IFolder) iResource;
				IFile ifile = iFolder.getFile(".saturn");
				SaturnDirectory saturnDirectory = SaturnDirectory
						.loadSaturnDirectory(ifile);

				if (saturnDirectory != null) {
					String saturnType = saturnDirectory.getType();

					if (saturnType.equalsIgnoreCase("action")) {
						newSaturnActionAction.setStructuredSelection(selection);
						submenu.add(newSaturnActionAction);
						submenu.add(new Separator());
						
					} else if (saturnType.equalsIgnoreCase("component")) {
						newSaturnComponentAction
								.setStructuredSelection(selection);
						submenu.add(newSaturnComponentAction);
					} else if (saturnType.equalsIgnoreCase("data")) {
						newSaturnDataAction.setStructuredSelection(selection);
						submenu.add(newSaturnDataAction);
					} else if (saturnType.equalsIgnoreCase("logic")) {
						newSaturnLogicAction.setStructuredSelection(selection);
						submenu.add(newSaturnLogicAction);
					} else if (saturnType.equalsIgnoreCase("web")) {
						newSaturnPageAction.setStructuredSelection(selection);
						submenu.add(newSaturnPageAction);
					} else if (saturnType.equalsIgnoreCase("workflow")) {
						newSaturnWorkFlowAction
								.setStructuredSelection(selection);
						submenu.add(newSaturnWorkFlowAction);
					} else if (saturnType.equalsIgnoreCase("validate")) {
						newSaturnValidateAction
								.setStructuredSelection(selection);
						submenu.add(newSaturnValidateAction);
					} else if (saturnType.equalsIgnoreCase("module")) {
						createActionModelAction
								.setStructuredSelection(selection);
						createLogicModelAction
								.setStructuredSelection(selection);
						createDataModelAction.setStructuredSelection(selection);
						createValidateModelAction
								.setStructuredSelection(selection);
						createWorkflowModelAction
								.setStructuredSelection(selection);
						createComponentModelAction
								.setStructuredSelection(selection);
						createPageModelAction.setStructuredSelection(selection);
						
						CreateSaturnSample.setStructuredSelection(selection);

						submenu.add(createActionModelAction);
						submenu.add(createLogicModelAction);
						submenu.add(createComponentModelAction);
						submenu.add(createDataModelAction);
						submenu.add(createValidateModelAction);
						submenu.add(createWorkflowModelAction);
						submenu.add(createPageModelAction);
						submenu.add(new Separator());
						submenu.add(CreateSaturnSample);
					}
				}
			}
			newWizardActionGroup.setContext(getContext());
			newWizardActionGroup.fillContextMenu(submenu);

			submenu.add(new Separator(ICommonMenuConstants.GROUP_ADDITIONS));
		}

		submenu.add(new Separator());
		submenu.add(showDlgAction);

		menu.insertAfter(ICommonMenuConstants.GROUP_NEW, submenu);
	}

	private void makeActions() {
		final PerformanceStats stats = PerformanceStats.getStats(null, this);
		stats.startRun();

		newSaturnActionAction = new NewSaturnActionAction();
		newSaturnValidateAction = new NewSaturnValidateAction();
		newSaturnComponentAction = new NewSaturnComponentAction();
		newSaturnDataAction = new NewSaturnDataItemAction();
		newSaturnLogicAction = new NewSaturnLogicAction();
		newSaturnPageAction = new NewSaturnPageAction();
		newSaturnWorkFlowAction = new NewSaturnWorkFlowAction();

		createActionModelAction = new CreateActionModelAction();
		createLogicModelAction = new CreateLogicModelAction();
		createComponentModelAction = new CreateComponentModelAction();
		createDataModelAction = new CreateDataModelAction();
		createValidateModelAction = new CreateValidateModelAction();
		createWorkflowModelAction = new CreateWorkflowModelAction();
		createPageModelAction = new CreatePageModelAction();
		CreateSaturnSample = new CreateSaturnSample();
		stats.endRun();
	}
}
